// axios拦截器
import axios from "axios";
import store from "store/index";
import { Toast } from "antd-mobile";

let isDev = process.env.NODE_ENV === "development";

let baseURL;
if (!isDev) {
  baseURL = "http://localhost:3006";
} else {
  baseURL = "http://81.71.65.4:3006";
}
const service = axios.create({
  baseURL,
  timeout: 10 * 60 * 1000,
});

//4. 请求拦截
service.interceptors.request.use(
  (config) => {
    console.log("post:", config.data);
    console.log("get:", config.params);
    let data = config.data || config.params;
    // skipLoading为true时不展示loading
    if (!data.skipLoading) {
      // 删除skipLoading属性
      Toast.show({
        duration: 0,
        icon: "loading",
        content: "加载中…",
      });
    }
    delete data.skipLoading;
    let token = store.token;
    // 暂时写死一个token,为了能拿数据
    config.headers["user-token"] = token;
    return config;
  },
  (err) => {
    Toast.show({
      icon: "fail",
      content: "网络异常,请稍后再试",
    });

    Toast.clear();
    // return Promise.reject(err);
  }
);

service.interceptors.response.use(
  (res) => {
    Toast.clear();
    const data = res.data;
    if (data.code * 1 === 666) {
      return data;
    } else { 

      Toast.show({
        icon: "fail",
        content: data.msg,
      });

    }
  },
  (err) => {
    Toast.show({
      icon: "fail",
      content: "网络异常,请稍后再试",
    });

    Toast.clear();
  }
); 


export default service;
